package com.backend.service.impl;

import com.backend.entity.Result;
import com.backend.entity.ResultCode;
import com.backend.entity.entity.Users;
import com.backend.entity.entity.WorkOrders;
import com.backend.mapper.UserMapper;
import com.backend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 作者:xuSen
 * 日期2025/7/19 11:18
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public Result getAllOrderByUserId(Integer userId) {
        List<WorkOrders> workOrdersList=userMapper.GetAllOrderByUserId(userId);
        if(workOrdersList.size()>0){
            return Result.success(workOrdersList);
        }
        else{
            return Result.failure(ResultCode.FAILURE,"请检查用户号！");
        }
    }

    @Override
    public Result Comment(int userid, int orderid, String feedback, int rating) {
        WorkOrders workOrders=userMapper.GetOrderByUseridAndOrderid(userid,orderid);
        if(workOrders==null){
            return Result.failure(ResultCode.FAILURE,"请检查订单号和用户号！");
        }
        else{
            userMapper.UpdateComment(userid,orderid,feedback,rating);
        }
        return Result.success();
    }

    @Override
    public Result commit(int userid, String description, String contactinfo, String deviceCode) {
        userMapper.commit(userid,description,contactinfo,deviceCode, LocalDateTime.now());
        int currentid=userMapper.getMaxid();
        String repairmanName=userMapper.GetNameByid(userid);

        WorkOrders workOrders=new WorkOrders();
        workOrders.setOrderId(currentid);
        workOrders.setUserId(userid);
        workOrders.setDescription(description);
        workOrders.setContactInfo(contactinfo);
        workOrders.setDeviceCode(deviceCode);
        workOrders.setRepairmanName(repairmanName);
        workOrders.setStatus(1);
        workOrders.setCreateTime(LocalDateTime.now());

        return Result.success(workOrders);
    }
}
